package com.zhoug.common3.anim;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.graphics.Point;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;

import com.zhoug.common3.utils.AppUtils;
import com.zhoug.common3.utils.LogUtils;
import com.zhoug.common3.utils.ReflectUtils;

import java.io.Serializable;
import java.lang.reflect.Method;

import androidx.annotation.Nullable;

/**
 * @Author: zhoug
 * @Date: 2024-08-12
 * @Description:
 */
public class ActivityAnim {
    private static final String TAG = ">>>ActivityAnim";
    private static final String PARAM_VIEW_POSITION = "viewPosition";


    public static void startActivity(Activity activity, View view, Intent intent) {
        ViewPosition viewPosition = new ViewPosition();
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        viewPosition.setLocationX(location[0]);
        viewPosition.setLocationY(location[1]);
        viewPosition.setWidth(view.getWidth());
        viewPosition.setHeight(view.getHeight());
        intent.putExtra(PARAM_VIEW_POSITION, viewPosition);
        activity.startActivity(intent);
        activity.overridePendingTransition(0, 0);
    }


    public static void startAnim(Activity activity, ViewPosition viewPosition, boolean start, Animation.AnimationListener animationListener) {
        if (activity == null) return;
        ViewGroup contentView = activity.findViewById(Window.ID_ANDROID_CONTENT);
        View animView = contentView;
        if (contentView != null) {
            //第一个子View ,即activity布局文件中的根layout
//            animView = contentView.getChildAt(0);
        }
        if (animView == null) return;
        LogUtils.d(TAG, "startAnim:viewPosition=" + viewPosition);
        Point screenSize = AppUtils.getScreenSize();
        int windowWidth = screenSize.x;
        int windowHeight = screenSize.y;
        LogUtils.d(TAG, "startAnim:windowWidth=" + windowWidth + ",windowHeight=" + windowHeight);
        float fromX = 0;
        float fromY = 0;
        float toX = 1.0f;
        float toY = 1.0f;
        float pivotX = 0;
        float pivotY = 0;
        if (viewPosition != null) {
            float locationX = viewPosition.getLocationX();
            float locationY = viewPosition.getLocationY();
            int width = viewPosition.getWidth();
            int height = viewPosition.getHeight();
            pivotX = locationX + (width >> 1);
            pivotY = locationY + (height >> 1);
//            pivotX = locationX;
//            pivotY = locationY;
            fromX = 1.0f * width / windowWidth;
            fromY = 1.0f * height / windowHeight;
        }
        fromX = 0;
        fromY = 0;
        if(!start){
            float temp=fromX;
            fromX=toX;
            toX=temp;
            temp=fromY;
            fromY=toY;
            toY=temp;
        }

        LogUtils.d(TAG, "startAnim:fromX=" + fromX +",toX="+toX+ ",fromY=" + fromY +",toY="+toY+ ",pivotX=" + pivotX + ",pivotY=" + pivotY);
//        pivotX=0;
//        pivotY=0;
        ScaleAnimation scaleAnimation = new ScaleAnimation(fromX, toX, fromY, toY,
                ScaleAnimation.ABSOLUTE, pivotX,
                ScaleAnimation.ABSOLUTE, pivotY);

        scaleAnimation.setDuration(400);
        scaleAnimation.setFillAfter(true);
        scaleAnimation.setAnimationListener(animationListener);
//        setTranslucent(activity, true);
        animView.startAnimation(scaleAnimation);

    }


    @Nullable
    public static ViewPosition getViewPosition(Activity activity) {
        if (activity == null) return null;
        Intent intent = activity.getIntent();
        ViewPosition viewPosition = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
            viewPosition = intent != null ? intent.getSerializableExtra(PARAM_VIEW_POSITION, ViewPosition.class) : null;
        } else {
            Serializable obj = intent != null ? intent.getSerializableExtra(PARAM_VIEW_POSITION) : null;
            if (obj instanceof ViewPosition) {
                viewPosition = (ViewPosition) obj;
            }
        }
        return viewPosition;
    }


    public static void setTranslucent(Activity activity, boolean translucent) {
        if (activity != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                activity.setTranslucent(translucent);
            } else {
                try {
                    if (translucent) {
                        Class<?>[] classes = Activity.class.getDeclaredClasses();
                        Class<?> translucentConversionListenerClazz = null;
                        for (Class clazz : classes) {
                            if (clazz.getSimpleName().contains("TranslucentConversionListener")) {
                                translucentConversionListenerClazz = clazz;
                            }
                        }
                        Method convertToTranslucentMethod = null;
                        convertToTranslucentMethod = Activity.class.getDeclaredMethod("convertToTranslucent",
                                new Class[]{translucentConversionListenerClazz, ActivityOptions.class});
                        convertToTranslucentMethod.setAccessible(true);
                        convertToTranslucentMethod.invoke(activity, null, null);
                        convertToTranslucentMethod.setAccessible(false);
                    } else {
                        ReflectUtils.invokeDeclaredMethod(Activity.class, "convertFromTranslucent", activity,
                                null, null);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }



}
